﻿using Coldairarrow.Business.PB;
using Coldairarrow.Entity.Base_Manage;
using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.PB;
using Coldairarrow.Util;
using Coldairarrow.Util.ClassLibrary;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Coldairarrow.Api.Controllers.PB
{
    [Route("/PB/[controller]/[action]")]
    public class PB_PackListController : BaseApiController
    {

        private readonly RedisOptions _redisOptions; //1.声明实体
        private readonly MongoDBOptions _mongodbOptions;
        #region DI

        public PB_PackListController(IPB_PackListBusiness pB_PackListBus, IOptions<MongoDBOptions> mongodbOptions, IOptions<RedisOptions> redisOptions)
        {
            _pB_PackListBus = pB_PackListBus;
            _mongodbOptions = mongodbOptions.Value;
            _redisOptions = redisOptions.Value;//3.赋值
        }

        IPB_PackListBusiness _pB_PackListBus { get; }

        #endregion

        #region 获取

        [HttpPost]
        public async Task<PageResult<PB_PackList>> GetDataList(PageInput<ConditionDTO> input)
        {
            return await _pB_PackListBus.GetDataListAsync(input);
        }

        [HttpPost]
        public async Task<PB_PackList> GetTheData(IdInputDTO input)
        {
            return await _pB_PackListBus.GetTheDataAsync(input.id);
        }

        /// <summary>
        /// PC包列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<PB_PackListDTO>> GetDataListPackDto(PageInput<PackListConditionDTO> input)
        {
            return await _pB_PackListBus.GetDataListPackDto(input);
        }
        [HttpPost]
        public async Task<PageResult<PB_PackListDTO>>GetDataListPackDtoXiang(PageInput<PackListConditionDTO> input){
            return await _pB_PackListBus.GetDataListPackDtoXiang(input);
       }

        /// <summary>
        /// 包位查询
        /// </summary>
        /// <param name="PackQrCode"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<PB_PackListDTO>> GetPackListQrCode(string PackQrCode) {
            return await _pB_PackListBus.GetPackListQrCode(PackQrCode);
        }
        /// <summary>
        /// 生成包编号
        /// </summary>
        /// <param name="userqrcode">操作人</param>
        /// <param name="PackageType">是否标准包</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PB_PackList> GetPackListNumberAsync(string userqrcode, int PackageType)
        {
            return await _pB_PackListBus.GetPackListNumberAsync(userqrcode, PackageType, _mongodbOptions);
        }


        /// <summary>
        /// 打包扫码添加打包明细(非混合包)
        /// </summary>
        /// <param name="qrcode">成品二维码</param>
        /// <param name="packId">包二维码</param>
        /// 优化打包逻辑   2022-03-02   赵锦涛
        /// <returns></returns>
        [HttpPost]
        public async Task<PB_PackListDetailDTO> GetPackListDetailDataAsync(string qrcode, string packId)
        {
            return await _pB_PackListBus.GetPackListDetailDataAsync(qrcode, packId);
        }

        /// <summary>
        /// PDA扫描第一个成品码后修改包的信息
        /// </summary>
        /// <param name="qrcode">成品二维码</param>
        /// <param name="packId">包二维码</param>
        /// <param name="IsMax">大/小</param>
        /// PDA打包增加选择大小包 2022-02-24 赵锦涛
        /// <returns></returns>
        [HttpPost]
        public async Task UpdatePackList(string qrcode, string packId, Boolean IsMax)
        {
            await _pB_PackListBus.UpdatePackListAsync(qrcode, packId, IsMax);
        }

        /// <summary>
        /// 打包扫码添加打包明细(混合包)
        /// </summary>
        /// <param name="qrcode">成品二维码</param>
        /// <param name="packId">包二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PB_PackListDetailDTO> GetBlendPackListDetailData(string qrcode, string packId)
        {
            return await _pB_PackListBus.GetBlendPackListDetailDataAsync(qrcode, packId);
        }

        /// <summary>
        /// PDA继续打包
        /// </summary>
        /// <param name="qrcode">成品二维码</param>
        /// <param name="packId">包二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PB_PackListDetailDTO> GetPackListDetailAsync(string qrcode, string packId)
        {
            return await _pB_PackListBus.GetPackListDetailAsync(qrcode, packId);
        }


        /// <summary>
        /// 获取包下的明细信息
        /// </summary>
        /// <param name="packId">包二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PB_PackListDataDTO> GetPackListDataAsync(string packId)
        {
            return await _pB_PackListBus.GetPackListDataAsync(packId);
        }

        /// <summary>
        /// 打印二维码选择箱号
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<List<PB_PackListDTO>> Selectid(string id) {
            var res = _pB_PackListBus.Selectid(id);
            return res;
        }
        /// <summary>
        /// 批量打印二维码箱号
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<List<PB_PackListDTO>> Selectids(string ids)
        {
            var res = _pB_PackListBus.Selectids(ids);
            return res;
        }


        /// <summary>
        /// 完成打包
        /// </summary>
        /// <param name="packId">包二维码</param>
        /// <param name="nums">数量</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PB_PackListDTO> UpdatePackListData(string packId, int nums)
        {
            return await _pB_PackListBus.UpdatePackListDataAsync(packId, nums);
        }

        /// <summary>
        /// 获取当前人的打包信息
        /// </summary>
        /// <param name="userqrcode">用户二维码</param>
        /// <param name="Type">包类型</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<PB_PackList>> GetPackListAsync(string userqrcode, int Type)
        {
            return await _pB_PackListBus.GetPackListAsync(userqrcode,Type);
        }

        /// <summary>
        /// 通知入库获取完成打包的数据
        /// </summary>
        /// <param name="packId">用户二维码</param>
        /// 优化PDA通知入库列表   2022-03-02   赵锦涛
        /// <returns></returns>
        [HttpPost]
        public async Task<List<PackListWarehousingNoticeDTO>> GetPackListInfo(string packId)
        {
            return await _pB_PackListBus.GetPackListWarehousDataAsync(packId);
        }

        /// <summary>
        /// 扫描获取要通知入库的单个成品
        /// </summary>
        /// <param name="JobQrCode">成品二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PackListWarehousingNoticeDTO> GetJobMaterialData(string JobQrCode)
        {
            return await _pB_PackListBus.GetJobMaterialDataAsync(JobQrCode);
        }

        /// <summary>
        /// 获取包下的打包完成的数据 手动输入
        /// </summary>
        /// <param name="packId"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<PB_PackList>> GetPackListInfoManualInputAsync(string packId)
        {
            return await _pB_PackListBus.GetPackListWarehousDataManualInputAsync(packId);
        }
        
        /// <summary>
        /// 获取需要入库操作的数据
        /// </summary>
        /// <param name="packId"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<PB_PackList>> GetPackListWarehousStatusAsync(string packId)
        {
            return await _pB_PackListBus.GetPackListWarehousStatusAsync(packId);
        }

        /// <summary>
        /// 获取需要入库操作的数据 手动输入
        /// </summary>
        /// <param name="ManualInput"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<PB_PackList>> GetPackListWarehousStatusManualInputAsync(string ManualInput)
        {
            return await _pB_PackListBus.GetPackListWarehousStatusManualInputAsync(ManualInput);
        }

        /// <summary>
        /// 删除打包明细信息
        /// </summary>
        /// <param name="packinfoId">打包明细Id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task DeletePackListInfoDataAsync(string packinfoId)
        {
            await _pB_PackListBus.DeletePackListInfoDataAsync(packinfoId);
        }

        /// <summary>
        /// 删除包信息
        /// </summary>
        /// <param name="packinfoId"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task DeletePackListDataAsync(string packinfoId)
        {
            await _pB_PackListBus.DeletePackListDataAsync(packinfoId);
        }

        [HttpPost]
        public async Task<PackListDetailDto> GetPackDetail(string QrCode) {
            return await _pB_PackListBus.GetPackDetail(QrCode);
        }
        /// <summary>
        /// PC获取包的明细信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<PB_PackListDetailDTO>> GetPackListInfoListData(PageInput<PackListInfoConditionDTO> input)
        {
            return await _pB_PackListBus.GetPackListInfoListDataAsync(input);
        }

        /// <summary>
        /// PC包明细成品码补打
        /// </summary>
        /// <param name="data"></param>
        /// 推送队列增加批次    2022-03-01  赵锦涛
        /// <returns></returns>
        [HttpPost]
        public async Task PackInfoPrint(PackInfoPrintData data)
        {
            await _pB_PackListBus.PackInfoPrintAsync(data, _mongodbOptions);
        }

        /// <summary>
        /// 获取操作者法人下的所有仓库
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<Base_Department>> GetDetpList()
        {
            return await _pB_PackListBus.GetDetpListAsync();
        }
        [HttpPost]
        public async Task<PageResult<ScanningCode>> GetscanningCodes(PageInput<IT_LocalDetailDtolistContionDTO> input, string PaickmainId)
        { 
        var list = await _pB_PackListBus.GetscanningCodes(input, PaickmainId);
            return list;
        }

        /// <summary>
        /// PDA补打包码或者物料码
        /// </summary>
        /// <param name="code">包(物料)Id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PrintPackOrMaterialDTO> GetPrintPackOrMaterialData(string code)
        {
            return await _pB_PackListBus.GetPrintPackOrMaterialDataAsync(code);
        }

        #endregion

        #region 提交
        /// <summary>
        /// 批量生成箱包编码
        /// </summary>
        /// <param name="counts"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task addrows(Print data) {
            await _pB_PackListBus.AddrowsAsyne(data, _mongodbOptions);
        }
        [HttpPost]
        public async Task SaveData(PB_PackList data)
        {
            if (data.Id.IsNullOrEmpty())
            {
                InitEntity(data);

                await _pB_PackListBus.AddDataAsync(data);
            }
            else
            {
                await _pB_PackListBus.UpdateDataAsync(data);
            }
        }
        /// <summary>
        /// 通知入库
        /// </summary>
        /// <param name="query">请求参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task NoticeWarehousing(NoticeWarehousingQuery query)
        {
            await _pB_PackListBus.NoticeWarehousing(query);

        }

        [HttpPost]
        public async Task DeleteData(List<string> ids)
        {
            await _pB_PackListBus.DeleteDataAsync(ids);
        }

        /// <summary>
        /// 拆包列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<PB_UnpackingDTO>> GetUnPackingList(PageInput<ConditionDTO> input)
        {
            return await _pB_PackListBus.GetUnPackingListAsync(input);
        }

        /// <summary>
        /// 拆包明细
        /// </summary>
        /// <param name="ParentId">拆包主表id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<PB_UnpackingDetailDTO>> GetUnPackingDetailListAsync(string ParentId)
        {
            return await _pB_PackListBus.GetUnPackingDetailListAsync(ParentId);
        }

        /// <summary>
        /// 批量生成包码
        /// </summary>
        /// <param name="data">数量</param>
        /// 推送队列增加批次    2022-03-01  赵锦涛
        /// <returns></returns>
        [HttpPost]
        public async Task AddBatchPackQrcode(Print data)
        {
            await _pB_PackListBus.AddBatchPackQrcodeAsync(data, _mongodbOptions);
        }

        /// <summary>
        /// 批量重打包码
        /// </summary>
        /// <param name="data"></param>
        /// 推送队列增加批次    2022-03-01  赵锦涛
        /// <returns></returns>
        [HttpPost]
        public async Task RetypeBatchPackQrcode(Print data)
        {
            await _pB_PackListBus.RetypeBatchPackQrcodeAsync(data, _mongodbOptions);
        }

        #endregion

        #region 初始化包数量

        /// <summary>
        /// 初始化包的数量
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task SavePackMaterialList()
        {
            await _pB_PackListBus.SavePackMaterialListAsync();
        }

        #endregion

        #region 盘点相关包、物料重置
        /// <summary>
        /// 重建包码
        /// </summary>
        /// <param name="strQrCode">扫描的二维码，可为空</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PB_PackList> RebuildBag(string strQrCode)
        {
            return await _pB_PackListBus.RebuildBagAsync(strQrCode);
        }

        /// <summary>
        /// 重建物料码
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <returns></returns>
        [HttpPost]
        public async Task<PB_JobBookingQrCode> RebuildMaterial(RebuildMaterialInputDTO input) 
        {
            return await _pB_PackListBus.RebuildMaterialAsync(input, _redisOptions);
        }

        /// <summary>
        /// 重建包提交
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task RebuildSubmit(RebuildSubmitInputDTO input)
        {
            await _pB_PackListBus.RebuildSubmitAsync(input);
        }

        /// <summary>
        /// 物料重置获取物料信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<JobBookMaterialDTO> GetJobBookMaterialData(RebuildMaterialInputDTO input)
        {
            return await _pB_PackListBus.GetJobBookMaterialDataAsync(input);
        }

        /// <summary>
        /// 生成物料码
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task SaveMaterialQrCode(MaterialQrCodeNewDTO data)
        {
            await _pB_PackListBus.SaveMaterialQrCodeAsync(data, _redisOptions, _mongodbOptions);
        }

        #endregion

        #region  查询包信息或者物料信息

        /// <summary>
        /// 查询包信息或者物料信息
        /// </summary>
        /// <param name="QrCode">二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Object> GetPackOrMaterialData(string QrCode)
        {
            return await _pB_PackListBus.GetPackOrMaterialDataAsync(QrCode);
        }

        #endregion

    }
}